package com.zyj.pool;

import com.vip.vjtools.vjkit.concurrent.threadpool.ThreadPoolBuilder;

import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 传统的FixedThreadPool有Queue但线程数量不变，而CachedThreadPool线程数可变但没有Queue
 * QueuableCachedThreadPool
 * 优先扩充线程池再扩充队列
 */
public class QueuableCachedThreadPoolSample {

    private static final ThreadPoolExecutor EXECUTOR = ThreadPoolBuilder.queuableCachedPool()
            .setMinSize(2) //线程启动就会创建的线程数, 并且会一直保持存在
            .setMaxSize(10) //最多的线程数
            .setQueueSize(100000) //队列的最大容量
            .setRejectHanlder(new ThreadPoolExecutor.AbortPolicy())
            .build();

    public static void main(String[] args) throws InterruptedException {
        for (int i = 0; i < 100; i++) {
            EXECUTOR.execute(() -> {
                try {
                    TimeUnit.HOURS.sleep(1);
                } catch (InterruptedException e) {
                }
            });
            TimeUnit.MILLISECONDS.sleep(200);
            System.out.println("pool size: " + EXECUTOR.getPoolSize());
            System.out.println("queue size: " + EXECUTOR.getQueue().size());
            System.out.println("---------------");
            TimeUnit.MILLISECONDS.sleep(200);
        }

    }
}
